package xyf.p01.array_nodelist;

import commons.ListNode;

/**
 * ClassName: P0234_Palindrome
 * Description:
 * Author: xyf
 * Date: 9/3/22 9:47 PM
 * Version: 1.0
 **/
public class P0234_Palindrome
{
    public boolean isPalindrome(ListNode head)
    {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
        }


        slow = reverse(slow);

        while (slow != null)
        {
            if (slow.val != head.val)
            {
                return false;
            }
            slow = slow.next;
            head = head.next;
        }
        return true;
    }

    private ListNode reverse(ListNode head)
    {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null)
        {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}
